from day_log import log_set
import os,time,re
from settings import g_adsl_account
def is_ok():
    cont=os.popen('ping baidu.com -c 3').read()
    if 'ttl' in cont or 'TTL' in cont:
        log_set('is_ok')
        return 1
    log_set('is_not_ok')
    print cont
class Adsl():
    def __init__(self):
        self.name = g_adsl_account["name"]
        self.username = g_adsl_account["username"]
        self.password = g_adsl_account["password"]

    def set_adsl(self, account):
        self.name = account["name"]
        self.username = account["username"]
        self.password = account["password"]
 
    def connect(self):
        try:

            cmd_str = "rasdial %s %s %s" % (self.name, self.username, self.password)
            print cmd_str
            os.system(cmd_str)
            time.sleep(5)
        except:
            print('link is fail')
    def disconnect(self):
        for i in range(2):
            cmd_str = "rasdial %s /disconnect" % self.name
            os.system(cmd_str)
        
        print cmd_str
    
    def reconnect2(self):
        while 1:
            log_set('do ppoe,sh')
            os.system('/usr/bin/sh /root/amazon_phantomjs_rank/pppoe.sh')
            time.sleep(5)
            if is_ok():
                log_set('have changg ip +++++++++++++++++++')
                return
    def reconnect(self):
        while 1:
            #if 'PPP' in str(os.popen('ipconfig').read()):
            if 1:
                self.disconnect()
                time.sleep(5)
                self.connect()
                time.sleep(1)
            if is_ok():
                break
if __name__=='__main__':
    A=Adsl()
    #if not is_ok():
    if 1:
        A.reconnect()
